package com.res.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.res.exam.HttpConnectionManager;
import com.res.exam.OnHttpConnectionLoad;

public class DemoDiplomActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	ImageButton okBtn;
	ImageButton exitBtn;
	EditText txt;
	TextView txtv;
	String aa;
	EditText userName;
	EditText pass;
	HttpConnectionManager manager;
	List<NameValuePair> params;
	ProgressDialog dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		okBtn = (ImageButton) findViewById(R.id.ok_Btn);
		exitBtn = (ImageButton) findViewById(R.id.exit_Btn);
		okBtn.setOnClickListener(this);
		exitBtn.setOnClickListener(this);
		userName = (EditText) findViewById(R.id.userName);
		pass = (EditText) findViewById(R.id.usePass);
		manager = new HttpConnectionManager();
		params = new ArrayList<NameValuePair>();

	}

	public void onClick(View arg0) {

		// TODO Auto-generated method stub
		if (arg0 == exitBtn) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Та гарахдаа итгэлтэй байна уу?")
					.setCancelable(false)
					.setPositiveButton("Тийм",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									System.exit(0);
								}
							})
					.setNegativeButton("Үгүй",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();

		}
		if (arg0 == okBtn) {
			// aa = txt.getText().toString();
			String name;
			String pass;
			dialog = new ProgressDialog(this);
			dialog.setTitle("Нэвтрэх");
			dialog.setMessage("шалгаж байна...");
			dialog.show();

			name = String.valueOf(userName.getText());
			pass = String.valueOf(this.pass.getText());
			if (name.length() > 0 && pass.length() > 0) {
				params.add(new BasicNameValuePair("BID", name));
				params.add(new BasicNameValuePair("pass", pass));
				manager.sentRequest("http://169.254.241.12/dip/login.php",
						params, new OnHttpConnectionLoad() {

							public void onHttpConnectionExecuted(String response) {
								// TODO Auto-generated method stub
								Log.d("RESPONSE", "no1 " + response);
								JSONObject a;
								try {
									a = new JSONObject(response);
									int k = a.getInt("StatusCode");
									dialog.dismiss();
									if (k == 0) {
										Intent i = new Intent(
												DemoDiplomActivity.this,
												MenuClassActivity.class);
										startActivity(i);
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}
						});

				Intent i = new Intent(this, MenuClassActivity.class);
				startActivity(i);
			}

			// txtv.setText(aa);
		}
	}

	public void onBackPressed() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Та гарахдаа итгэлтэй байна уу?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								System.exit(0);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();

	}
}